package nina.yiyou.salary.input.config


import nina.yiyou.salary.input.util.ExcelReadOpener
import nina.yiyou.salary.input.util.InputPathResolver
import org.apache.poi.ss.usermodel.Workbook

import java.nio.file.Path

class ConfigExcelReader {

  interface Result {

    Map<String, String> global()

    /**
     * 各部门上下班时间
     */
    List<WorkTimeSheetReader.Line> workTime()
  }

  static ConfigExcelReader create(Path workshopPath) {
    Workbook workbook = new ExcelReadOpener(workshopPath).openXlsx()
    Map globalMap = new GlobalSheetReader(workbook).read()
    workbook.close()
    return new ConfigExcelReader(globalMap, workshopPath)
  }

  ConfigExcelReader(Map globalMap, Path workshopPath) {
    _globalMap = globalMap
    _workshopPath = workshopPath
  }

  Result read() {
    def pathCfg = _globalMap['设置'] as String
    Path path = new InputPathResolver(_workshopPath, pathCfg).resolve()
    Workbook workbook = new ExcelReadOpener(path).openXlsx()

    Map globalMap = new GlobalSheetReader(workbook).read()
    List workTimeCfg = new WorkTimeSheetReader(workbook).read()
    workbook.close()

    return new Result() {
      @Override
      Map global() {
        return globalMap
      }

      @Override
      List<WorkTimeSheetReader.Line> workTime() {
        return workTimeCfg
      }
    }
  }

  private final Map _globalMap

  private final Path _workshopPath
}
